import { SCREEN_WIDTH, SCREEN_HEIGHT } from '../common/constants.js'

export default class Ground extends Sprite {
  constructor() {
    super('images/ground.jpg', SCREEN_WIDTH, 100);
    
    this.x = 0;
    this.y = SCREEN_HEIGHT - this.height;
    
    // 设置渲染尺寸
    this.renderWidth = SCREEN_WIDTH;
    this.renderHeight = this.height;
    
    // 设置移动速度（可以根据需要调整）
    this.speed = 2;

    // 检查图片加载
    this.img.onload = () => {
      console.log('Ground image loaded successfully:', {
        imgWidth: this.img.width,
        imgHeight: this.img.height
      });
    };

    this.img.onerror = () => {
      console.error('Failed to load ground image');
    };

    // 添加调试日志
    console.log('Ground initialized:', {
      x: this.x,
      y: this.y,
      width: this.renderWidth,
      height: this.renderHeight,
      screenHeight: SCREEN_HEIGHT,
      imageSource: this.img.src
    });
  }

  update() {
    // 向上移动地面
    this.y += this.speed;
    
    // 当移出屏幕顶部时重置位置
    if (this.y >= SCREEN_HEIGHT) {
      this.y = SCREEN_HEIGHT - this.renderHeight;
    }
  }

  render(ctx) {
    // 添加调试代码，绘制一个红色矩形来显示地面位置
    ctx.fillStyle = 'red';
    ctx.fillRect(this.x, this.y, this.renderWidth, this.renderHeight);
    
    // 绘制第一张地面
    ctx.drawImage(
      this.img,
      0,
      0,
      this.img.width,
      this.img.height,
      this.x,
      this.y,
      this.renderWidth,
      this.renderHeight
    );

    // 绘制第二张地面（无缝连接）
    ctx.drawImage(
      this.img,
      0,
      0,
      this.img.width,
      this.img.height,
      this.x + this.renderWidth,
      this.y,
      this.renderWidth,
      this.renderHeight
    );

    // 添加调试信息
    console.log('Ground rendering:', {
      x: this.x,
      y: this.y,
      renderWidth: this.renderWidth,
      renderHeight: this.renderHeight
    });
  }
} 